﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdmLaboratorios.Business;
using AdmLaboratorios.DTO;

namespace AdmLaboratorios
{
    public partial class frmPartner : Form
    {
        public frmPartner()
        {
            InitializeComponent();
        }

        private void frmPartner_Load(object sender, EventArgs e)
        {
            //gv.DataSource = Recepcion.Partners();

            gv.DataSource = null;
            gv.DataSource = Partners.PartnersFiltered(txPartner.Text);
        }

        private void txPartner_TextChanged(object sender, EventArgs e)
        {
            gv.DataSource = null;
            gv.DataSource = Partners.PartnersFiltered(txPartner.Text);
        }

        private void btNuevoPartner_Click(object sender, EventArgs e)
        {
            using (frmPartnerABM f = new frmPartnerABM())
            {
                f.Partner = new PartnerDTO();
                FormsHelper.CargarColumnasGrilla(f);
                if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ReLoad();
                }
            }
        }

        private void btModificarPartner_Click(object sender, EventArgs e)
        {
            if (gv.SelectedRows.Count == 1)
            {
                using (frmPartnerABM f = new frmPartnerABM())
                {
                    f.Partner = (PartnerDTO)gv.SelectedRows[0].DataBoundItem;

                    FormsHelper.CargarColumnasGrilla(f);
                    if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        ReLoad();
                    }

                }
            }
        }

        private void ReLoad()
        {
            gv.DataSource = null;
            gv.DataSource = Partners.PartnersFiltered(txPartner.Text);
        }

        private void btEliminarPartner_Click(object sender, EventArgs e)
        {
            try
            {
                if (gv.SelectedRows.Count == 1)
                {
                    if (MessageBox.Show(
                        "Está a punto de eliminar el Partner seleccionado. Esta operación no puede deshacerse. ¿Desea continuar?",
                        "Eliminar Partner",
                        MessageBoxButtons.OKCancel,
                        MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
                    {
                        PartnerDTO partner = (PartnerDTO)gv.SelectedRows[0].DataBoundItem;

                        //Partners.Eliminar(partner.Id);
                        bool eliminada = Partners.Eliminar(partner.Id);

                        if (eliminada)
                        {
                            MessageBox.Show("El Partner se eliminó correctamente.",
                                "Operación Completada", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            gv.DataSource = null;
                            gv.DataSource = Recepcion.Partners();
                        }
                        else
                        {
                            MessageBox.Show("No se puede eliminar el Partner, ya que existen registros relacionados.",
                                "Imposible Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                FormsHelper.MsgErrorInesperado(ex);
            }

            //if (gv.SelectedRows.Count == 1)
            //{
            //    if (MessageBox.Show(
            //                "¿Está seguro de que desea eliminar el Partner seleccionado?",
            //                "Eliminar Contacto", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            //    {
            //        PartnerDTO partner = (PartnerDTO)gv.SelectedRows[0].DataBoundItem;

            //        Partners.Eliminar(partner.Id);

            //        gv.DataSource = null;
            //        gv.DataSource = Recepcion.Partners();
            //    }
            //}
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
